
Partial Class DashboardControls_MasterParticipantList
    Inherits System.Web.UI.UserControl
    Implements IDashBoard

    Private titleLabelString As String = ""
    Dim pageSizeValue As Integer

    Protected Property GridViewSortDirection() As String
        Get
            If ViewState("SortDirection") Is Nothing Then
                ViewState("SortDirection") = SortDirection.Ascending
            End If
            Return CType(ViewState("SortDirection"), SortDirection)
        End Get
        Set(ByVal value As String)
            ViewState("SortDirection") = value
        End Set
    End Property

    Public Property SortDirection() As System.Web.UI.WebControls.SortDirection Implements IDashBoard.SortDirection
        Get
            Return GridViewSortDirection
        End Get
        Set(ByVal value As System.Web.UI.WebControls.SortDirection)
            GridViewSortDirection = value
        End Set
    End Property

    Public Property PageNumber() As Integer Implements IDashBoard.PageNumber
        Get
            Return Me.MasterParticipantsGridView.PageIndex
        End Get
        Set(ByVal value As Integer)
            Me.MasterParticipantsGridView.PageIndex = value
        End Set
    End Property

    Public Property PageSize() As Integer Implements IDashBoard.PageSize
        Get
            If Not Me.MasterParticipantsGridView Is Nothing Then
                If Me.MasterParticipantsGridView.AllowPaging = False Then
                    Return -1
                Else
                    Return Me.MasterParticipantsGridView.PageSize
                End If

            Else
                Return pageSizeValue
            End If
        End Get
        Set(ByVal value As Integer)
            If Not Me.MasterParticipantsGridView Is Nothing Then
                If value = -1 Then
                    Me.MasterParticipantsGridView.AllowPaging = False
                Else
                    Me.MasterParticipantsGridView.AllowPaging = True
                    Me.MasterParticipantsGridView.PageSize = value
                End If

                Me.NumberOfRowsDropDownList.SelectedValue = value.ToString()
            End If
            pageSizeValue = value
        End Set
    End Property

    Public Property SortField() As String Implements IDashBoard.SortField
        Get
            If ViewState("SortField") Is Nothing Then
                Return ""
            Else
                Return ViewState("SortField")
            End If
        End Get
        Set(ByVal value As String)
            ViewState("SortField") = value
        End Set
    End Property


    Public Property Title() As String Implements IDashBoard.Title
        Get
            If Me.TitleLabel IsNot Nothing Then
                Return TitleLabel.Text
            Else
                Return titleLabelString
            End If
        End Get
        Set(ByVal value As String)
            If TitleLabel IsNot Nothing Then
                TitleLabel.Text = value
            End If
            titleLabelString = value
        End Set
    End Property

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        AddHandler NumberOfRowsDropDownList.SelectedIndexChanged, AddressOf NumberOfRowsDropDownList_SelectedIndexChanged
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


        If Not String.IsNullOrEmpty(titleLabelString) Then
            Me.TitleLabel.Text = titleLabelString
        End If

        If pageSizeValue > 0 Then
            Me.PageSize = pageSizeValue
        End If

        If Not Me.IsPostBack Then
            GridViewSettingController.SetGridViewValues(Me, 5, "DisplayLastNameFirst", WebControls.SortDirection.Ascending)
            Me.PageSize = GridViewSettingController.GetNumberOfRows(Me, 5)
            Me.BindGrid()
        End If

    End Sub

    Protected Sub NumberOfRowsDropDownList_SelectedIndexChanged(ByVal sender As Object, _
                                                                ByVal e As System.EventArgs) _
                                                                Handles NumberOfRowsDropDownList.SelectedIndexChanged

        If NumberOfRowsDropDownList.SelectedValue = "-1" Then
            Me.MasterParticipantsGridView.AllowPaging = False
        Else
            MasterParticipantsGridView.AllowPaging = True
            MasterParticipantsGridView.PageSize = NumberOfRowsDropDownList.SelectedValue
            MasterParticipantsGridView.PageIndex = 0
        End If

        Me.BindGrid()

    End Sub

    Public Sub BindGrid() Implements IDashBoard.BindGrid

        Dim isAscending As Boolean = (Me.SortDirection.Equals(SortDirection.Ascending))
        Me.MasterParticipantsGridView.DataSource = Cdm.DataCore.Custom.ParticipantDisplay.ListActiveByUserId(Session("CurrentUserId"), Me.SortField, isAscending)

        If Me.NumberOfRowsDropDownList.SelectedValue <> "-1" Then
            Me.MasterParticipantsGridView.PageSize = Me.NumberOfRowsDropDownList.SelectedValue
        End If
        Me.MasterParticipantsGridView.DataBind()
        GridViewSettingController.SaveSetting(Me)

    End Sub

    Protected Sub MasterParticipantsGridView_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles MasterParticipantsGridView.DataBound
        AccessibilityController.AddImageToSortableGridHeaders(MasterParticipantsGridView)
    End Sub

    Protected Sub MasterParticipantsGridView_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles MasterParticipantsGridView.PageIndexChanging
        Me.PageNumber = e.NewPageIndex
        Me.BindGrid()
    End Sub

    Public Sub LoadControl1(ByVal sender As Object, ByVal e As System.EventArgs) Implements IDashBoard.LoadControl
        Me.Page_Load(sender, e)
    End Sub

    Protected Sub MasterParticipantsGridView_Sorting(ByVal sender As Object, _
                                                     ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) _
                                                     Handles MasterParticipantsGridView.Sorting

        Dim sortField As String = e.SortExpression
        If sortField.Equals("LastName") Then
            sortField = "LastName,FirstName"
        End If

        If GridViewSortDirection = SortDirection.Ascending And sortField = Me.SortField Then
            GridViewSortDirection = SortDirection.Descending
            Me.MasterParticipantsGridView.DataSource = Cdm.DataCore.Custom.ParticipantDisplay.ListActiveByUserId(Session("CurrentUserId"), sortField, False)
        Else
            GridViewSortDirection = SortDirection.Ascending
            Me.MasterParticipantsGridView.DataSource = Cdm.DataCore.Custom.ParticipantDisplay.ListActiveByUserId(Session("CurrentUserId"), sortField, True)
        End If

        If Me.NumberOfRowsDropDownList.SelectedValue <> -1 Then
            Me.MasterParticipantsGridView.PageSize = Me.NumberOfRowsDropDownList.SelectedValue
        End If
        Me.SortField = sortField
        Me.MasterParticipantsGridView.DataBind()
        GridViewSettingController.SaveSetting(Me)

    End Sub

    Public Function GetLabel(ByVal labelName As String) As String
        Return EntityController.ReplaceDisplayName(labelName)
    End Function

End Class
